﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using YY.Common;
using YY.UI.BaseDataSvc;

namespace YY.UI
{
    public partial class BaseFrm : Form
    {
        /// <summary>
        /// 定义日志对象
        /// </summary>
        //public static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        bool _IsPermitFrm = true;
        /// <summary>
        /// 是否授权限制窗体
        /// </summary>
        public bool IsPermitFrm { get { return this._IsPermitFrm; } set { this._IsPermitFrm = value; } }

        /// <summary>
        /// 当前用户模块权限数据
        /// </summary>
        public IEnumerable<TB_ActionPermission> UserPermitDatas = null;

        /// <summary>
        /// 当前窗体所属模块Id,默认null不指定模块Id，使用窗体id作条件绑定权限数据
        /// </summary>
        public int? ModuleId;

        //BaseDataSvc.BaseDataClient _BaseDataProxy = null;
        /// <summary>
        /// 业务数据接口代理
        /// </summary>
        public BaseDataSvc.BaseDataClient BaseDataProxy
        {
            get;
            set;
            //get
            //{
            //    if (this._BaseDataProxy == null)
            //    {
            //        this._BaseDataProxy = new BaseDataClient();
            //        this._BaseDataProxy.Open();
            //    }
            //    return this._BaseDataProxy;
            //}
            //set { this._BaseDataProxy = value; }
        }

        public BaseFrm()
        {
            InitializeComponent();
            this.skinEngine1.SkinFile = "CalmnessColor1.ssk";//"CalmnessColor1.ssk";
            //this.KeyPreview = true;
            //this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //授权操作按钮可用状态控制
            if (this.IsPermitFrm && !SysLoginUserInfo.IsAdministrator && this.BaseDataProxy != null)
            {
                this.UserPermitDatas = this.BaseDataProxy.FindActionPermissions(true, SysLoginUserInfo.Role, this.ModuleId, this.Name);
                initActionCtrls(this.Controls, this.UserPermitDatas);
            }
            this.KeyPreview = true;
        }        

        /// <summary>
        /// 初始化窗体操作按钮可用状态
        /// </summary>
        /// <param name="ctrls"></param>
        /// <param name="objs"></param>
        protected void initActionCtrls(Control.ControlCollection ctrls, IEnumerable<TB_ActionPermission> objs)
        {
            foreach (Control ctrl in ctrls)
            {
                if (ctrl.HasChildren)
                    initActionCtrls(ctrl.Controls, objs);
                else
                {
                    if (ctrl is ToolStrip)
                    {
                        ToolStrip tl = ctrl as ToolStrip;
                        foreach (ToolStripItem tsi in tl.Items)
                        {
                            if (tsi.Tag != null && Enum.IsDefined(typeof(EnumCommon.PermissionOpType), tsi.Tag))
                            { tsi.Enabled = tsi.Enabled & Common.IsActionPermit(objs, (EnumCommon.PermissionOpType)Enum.Parse(typeof(EnumCommon.PermissionOpType), tsi.Tag.ToString())); }

                        }
                    }
                    else if (ctrl is Button && ctrl.Tag != null && Enum.IsDefined(typeof(EnumCommon.PermissionOpType), ctrl.Tag))
                        ctrl.Enabled = ctrl.Enabled & Common.IsActionPermit(objs, (EnumCommon.PermissionOpType)Enum.Parse(typeof(EnumCommon.PermissionOpType), ctrl.Tag.ToString()));
                }
            }
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            if (this.BaseDataProxy != null)
            {
                if (this.BaseDataProxy.State != System.ServiceModel.CommunicationState.Closed && this.BaseDataProxy.State != System.ServiceModel.CommunicationState.Faulted)
                    this.BaseDataProxy.Close();
                this.BaseDataProxy = null;
            }

            //if (LOG != null)
            //    LOG = null;
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            //base.OnKeyPress(e);
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (!this.ActiveControl.GetType().Equals(typeof(ComboBox)) && !this.ActiveControl.GetType().BaseType.Equals(typeof(BaseFrm)))//跳过指定控件类型                                    
                {
                    SelectNextControl(this.ActiveControl, true, true, true, true);
                    //e.Handled = true;
                }
            }
        }

    }
}
